/**$Licence:
 *  Copyright (C) 2008 - 2012 www.java1995.com . All rights reserved.
 *
 *  Use this however/wherever you like, just don't blame me if it breaks anything.
 *
 */

package com.blazemina.config;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.mina.core.service.IoHandler;

import com.blazegame.logging.Logger;
import com.blazegame.logging.LoggerFactory;
import com.blazemina.container.RequestProcessor;
import com.blazemina.mina.handler.RequestHandlerHelper;
import com.blazemina.util.ClassUtils;

/**
 * @author JackChu
 * @version 1.0.0 2012-2-6 下午10:10:19
 */
public class ServiceAcceptor {

	private static final Logger log = LoggerFactory
			.getLogger(ServiceAcceptor.class);

	private String serviceName;
	private String localHost;
	private int localPort;
	private Map<String, ServiceFilter> filters = new HashMap<String, ServiceFilter>();
	private String sessionHandle;
	private RequestHandlerHelper requestHandler;
	private String serviceHandle = "com.blazemina.mina.handler.ServerHandler";
	private IoHandler ioHandler;
	private String commandFactory = "com.blazemina.command.DefaultJavaCommandFactory";

	private String requestProcessor = "com.blazemina.container.DefaultRequestProcessor";

	/**
	 * @return the requestProcessor
	 */
	public String getRequestProcessor() {
		return requestProcessor;
	}

	/**
	 * @param requestProcessor
	 *            the requestProcessor to set
	 */
	public void setRequestProcessor(String requestProcessor) {
		this.requestProcessor = requestProcessor;
	}

	/**
	 * @param requestHandler
	 *            the requestHandler to set
	 */
	public void setRequestHandler(RequestHandlerHelper requestHandler) {
		this.requestHandler = requestHandler;
	}

	/**
	 * serviceName
	 * 
	 * @return the serviceName
	 */

	public String getServiceName() {
		return serviceName;
	}

	/**
	 * serviceName
	 * 
	 * @param serviceName
	 *            the serviceName to set
	 */

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	/**
	 * localHost
	 * 
	 * @return the localHost
	 */

	public String getLocalHost() {
		return localHost;
	}

	/**
	 * localHost
	 * 
	 * @param localHost
	 *            the localHost to set
	 */

	public void setLocalHost(String localHost) {
		this.localHost = localHost;
	}

	/**
	 * localPort
	 * 
	 * @return the localPort
	 */

	public int getLocalPort() {
		return localPort;
	}

	/**
	 * localPort
	 * 
	 * @param localPort
	 *            the localPort to set
	 */

	public void setLocalPort(int localPort) {
		this.localPort = localPort;
	}

	/**
	 * serviceHandle
	 * 
	 * @return the serviceHandle
	 */

	public String getServiceHandle() {
		return serviceHandle;
	}

	/**
	 * serviceHandle
	 * 
	 * @param serviceHandle
	 *            the serviceHandle to set
	 */
	public void setServiceHandle(String serviceHandle) {
		if (StringUtils.isBlank(serviceHandle)) {
            throw new ConfigInitializeException("service handler cannot be null");
        }
		this.serviceHandle = serviceHandle;
	}

	/**
	 * filters
	 * 
	 * @return the filters
	 */
	public Map<String, ServiceFilter> getFilters() {
		return filters;
	}

	public void addFilter(ServiceFilter filter) {
		String message = null;
		if (null == filter.getName() || "".equals(filter.getName())
				|| null == filter.getClassName()
				|| "".equals(filter.getClassName())) {
			message = "initialize filter error,the filter name or filter className is null";
			log.error(message);
			throw new ConfigInitializeException(message);
		} else if (filters.containsKey(filter.getName())) {
			message = "initialize filter error,the duplicate filter name:"
					+ filter.getName();
			log.error(message);
			throw new ConfigInitializeException(message);
		} else {
			filters.put(filter.getName(), filter);
			log.debug("accepter config filter,add filter:" + filter.getName());
		}

	}

	/**
	 * sessionHandle
	 * 
	 * @return the sessionHandle
	 */

	public String getSessionHandle() {
		return sessionHandle;
	}

	/**
	 * sessionHandle
	 * 
	 * @param sessionHandle
	 *            the sessionHandle to set
	 */

	public void setSessionHandle(String sessionHandle) {
		this.sessionHandle = sessionHandle;
	}

	public RequestHandlerHelper getRequestHandler() {
		if (requestHandler == null) {
			try {
				requestHandler = (RequestHandlerHelper) ClassUtils
						.applicationInstance(sessionHandle);
			} catch (Exception e) {
				log.error("create RequestHandler error, sessionHandle:"
						+ sessionHandle, e);
			}
		}
		return requestHandler;
	}

	public IoHandler getServerHandler() {
		if (ioHandler == null) {
			try {
				ioHandler = (IoHandler)ClassUtils.applicationInstance(serviceHandle);
			} catch (Exception e) {
				log.error("create IoHandler error, IoHandler:" + serviceHandle,
						e);
				e.printStackTrace();
			}
		}
		return ioHandler;
	}

	/**
	 * @return the commandFactory
	 */
	public String getCommandFactory() {
		return commandFactory;
	}

	/**
	 * @param commandFactory
	 *            the commandFactory to set
	 */
	public void setCommandFactory(String commandFactory) {
		this.commandFactory = commandFactory;
	}

}
